/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.library.backend;

import cz.muni.fi.pa165.library.api.UserRole;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.PersistenceContext;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author Filip Ligac
 */
@Repository
@Transactional
public class UserDAOImpl implements UserDAO {

    @PersistenceContext(name = "LibraryPU")
    private EntityManager entityManager;

    @PersistenceContext
    public void setEntityManager(EntityManager entityManager) {
        this.entityManager = entityManager;
    }

    private void checkParameters(User user) {
        if (user == null) {
            throw new IllegalArgumentException("User cannot be null.");
        }
        if (user.getEmail() == null) {
            throw new IllegalArgumentException("Email cannot be null.");
        }
        if (user.getEmail().compareTo("") == 0) {
            throw new IllegalArgumentException("Email cannot be empty.");
        }
        if (user.getPassword() == null) {
            throw new IllegalArgumentException("User's password cannot be null.");
        }
        if (user.getPassword().compareTo("") == 0) {
            throw new IllegalArgumentException("User's password cannot be empty.");
        }
         if (user.getLastName() == null) {
            throw new IllegalArgumentException("Last name cannot be null.");
        }
        if (user.getLastName().compareTo("") == 0) {
            throw new IllegalArgumentException("Last name cannot be empty.");
        }
         if (user.getFirstName() == null) {
            throw new IllegalArgumentException("First name cannot be null.");
        }
        if (user.getFirstName().compareTo("") == 0) {
            throw new IllegalArgumentException("First name cannot be empty.");
        }
    }


    public User createUser(User user) {
        checkParameters(user);
        if (user.getUserID() != null) {
            throw new IllegalArgumentException("User id is already set.");
        }
        if (!(findUserByEmail(user.getEmail()) == null)) {
            throw new IllegalArgumentException("There is already a user with such email.");
        }   
        entityManager.persist(user);
        return user;
    }

    public User findUserByEmail(String email) throws IllegalArgumentException {
        if (email == null) {
            throw new IllegalArgumentException("Email is null.");
        }
        
        String query = "SELECT e FROM User e WHERE e.email = '" + email + "'";
        try {
            return entityManager.createQuery(query, User.class).getSingleResult();
        } catch (NoResultException e) {
            return null;
        } 
    }
   

    public void deleteUser(User user) {
        if (user == null) {
            throw new IllegalArgumentException("Deleted user is null.");
        }
        if (user.getUserID() == null) {
            throw new IllegalArgumentException("Deleted user id is null.");
        }
        User newEntity = entityManager.merge(user);
        entityManager.remove(newEntity);
    }

    public void updateUser(User user) {
        checkParameters(user);
        User user2 = findUserById(user.getUserID());     
        if (user2 != null) {
            user2.setEmail(user.getEmail());
            user2.setFirstName(user.getFirstName());
            user2.setLastName(user.getLastName());
            user2.setPassword(user.getPassword());
        } else {
            throw new IllegalArgumentException("Unknown user id:" + user.getUserID());
        }
        entityManager.merge(user2);
    }

    public User findUserById(Long id) {
        if (id == null) {
            throw new IllegalArgumentException("Id searched user is null");
        }
        if (id.intValue() <= 0) {
            throw new IllegalArgumentException("Id searched user is not in correct format");
        }
        User user = entityManager.find(User.class, id);
        if (user == null) {
            throw new IllegalArgumentException("Unknown used id:" + id);
        }
        return user;
    }
}

